﻿using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Windows;
using VideoLib.Config;
using VideoLib.ViewModels;
using Application = System.Windows.Application;

namespace VideoLib
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            DirectoryInfo directoryInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\temp");
            directoryInfo.GetFiles("*.png").ToList().ForEach(f => f.Delete());

            var pros = Process.GetProcessesByName("ffmpeg");
            for (int i = 0; i < pros.Length; i++)
            {
                var pro = pros[i];
                pro.Kill();
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            SetTheme(AppConfig.Instance.Theme);
        }

        public static void SetTheme(string theme)
        {
            var res = Application.Current.Resources.MergedDictionaries;
            res.RemoveAt(0);
            res.Insert(0, new ResourceDictionary() { Source = new Uri($"../Styles/Theme{theme}.xaml", UriKind.Relative) });
            AppConfig.Instance.Theme = theme;
            AppConfig.Save();
        }

        public static MainViewModel MainViewModel { get; set; }
    }
}
